package com.py.credit.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.entity.LegalPersonTable;
import com.py.credit.service.LegalPersonTableService;
import com.py.framework.core.common.Page;

/**
 * 法人库 表的操作
 * cronExpParser
 * @author zhou
 *
 */
@RestController
@RequestMapping(value = "/LegalPersonTable")
public class LegalPersonTableController extends BaseController {

	@Autowired
	private LegalPersonTableService legalPersonTableService;

	// 获取法人库中关联表数据
	@RequestMapping(value = "/getAllTableList.json")
	public RestServiceResult<List<LegalPersonTable>> getAllTableList() {
		return new RestServiceResult<List<LegalPersonTable>>(legalPersonTableService.getAllTableList());

	}

	// 条件获取
	@RequestMapping(value = "/getAllTableListByTitle.json")
	public RestServiceResult<List<LegalPersonTable>> getAllTableListByTitle(
			@RequestParam(value = "title", defaultValue = "", required = false) String title) {
		return new RestServiceResult<List<LegalPersonTable>>(legalPersonTableService.getAllTableListByTitle(title));

	}

	// 获取t_legalPersonTable已经有的关联数据
	@RequestMapping(value = "/getIdTableList.json")
	public RestServiceResult<List<LegalPersonTable>> getIdTableList() {
		return new RestServiceResult<List<LegalPersonTable>>(legalPersonTableService.getIdTableList());

	}

	// 分页获取t_legalPersonTable已经有的关联数据
	@RequestMapping(value = "/getIdTableListForPage.json")
	public RestServiceResult<List<LegalPersonTable>> getIdTableListForPage(Page page) {
		return new RestServiceResult<List<LegalPersonTable>>(legalPersonTableService.getIdTableListForPage(page));

	}

	// 移除单条数据
	@RequestMapping(value = "/delLegalPersonTableById.json")
	public RestServiceResult<Object> delLegalPersonTableById(
			@RequestParam(value = "id", defaultValue = "", required = true) String id) {
		RestServiceResult<Object> result = new RestServiceResult<Object>();
		try {
			legalPersonTableService.delLegalPersonTableById(id);
			result.setCode(RestServiceResult.CODE_SUCCESS);
			result.setMsg("删除成功");
		} catch (Exception e) {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("删除失败");
		}
		return result;

	}

	/**
	 * 功能描述: 批量移除数据
	 *
	 * @author zhouquan
	 * @date 19/4/2 15:16
	 */
	@RequestMapping(value = "/delLegalPersonTableByIdList.json")
	public RestServiceResult<Object> delLegalPersonTableByIdList(
			@RequestParam(value = "ids", defaultValue = "", required = true) String strId) {
		List<String> idList = java.util.Arrays.asList(strId.split(","));
		RestServiceResult<Object> result = new RestServiceResult<Object>();
		try {
			legalPersonTableService.delLegalPersonTableByIdList(idList);
			result.setCode(RestServiceResult.CODE_SUCCESS);
			result.setMsg("删除成功");
		} catch (Exception e) {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("删除失败");
		}
		return result;

	}

	// 清空表数据 插入新数据
	@RequestMapping(value = "/insertLegalPersonTable.json")
	public RestServiceResult<Object> insertLegalPersonTable(
			@RequestParam(value = "strId", defaultValue = "", required = true) String strId) {
		List<String> idList = new ArrayList<>();
		if (strId.isEmpty()) {
			idList = null;
		} else {
			idList = java.util.Arrays.asList(strId.split(","));
		}
		RestServiceResult<Object> result = new RestServiceResult<Object>();
		try {
			legalPersonTableService.insertLegalPersonTable(idList);
			result.setCode(RestServiceResult.CODE_SUCCESS);
			result.setMsg("添加成功");
		} catch (Exception e) {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("添加失败");
		}
		return result;
	}

	// 分页获取t_legalPersonTable已经有的关联数据
	@RequestMapping(value = "/getIdTableListByTitleForPage.json")
	public RestServiceResult<Object> getIdTableListByTitleForPage(
			@RequestParam(value = "title", defaultValue = "", required = false) String title, Page page) {
		RestServiceResult<Object> result = new RestServiceResult<Object>();
		List<LegalPersonTable> tableList = legalPersonTableService.getIdTableListByTitleForPage(title, page);
		result.setData(tableList);
		result.setCount(page.getTotalCount());
		return result;

	}
}
